<?php
namespace app\controller\aiAvatar;

use app\BaseController;
use app\service\aiAvatar\VideoAvatarService;
use think\facade\Validate;
use think\App;

class Task extends BaseController
{
    protected $videoAvatarService;

    public function __construct(App $app, VideoAvatarService $videoAvatarService)
    {
        parent::__construct($app);
        $this->videoAvatarService = $videoAvatarService;
    }

    /**
     * 提交视频生成任务
     */
    public function videoAvatarSubmit()
    {
        try {
            $params = input('post.');
            $user = $this->request->user;
            if (!$user) {
                return json(['code' => 401, 'msg' => '用户未登录']);
            }
            $data = $this->videoAvatarService->createVideoAvatarTask($user->uid, $params);
            return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 查询任务状态
     */
    public function videoAvatarQuery()
    {
        try {
            $taskId = input('taskId');
            $data = $this->videoAvatarService->queryTask($taskId);
            return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取任务列表
     */
    public function videoAvatarList()
    {
        try {
            $page = input('page/d', 1);
            $pageSize = input('pageSize/d', 10);
            $user = $this->request->user;
            if (!$user) {
                return json(['code' => 401, 'msg' => '用户未登录']);
            }
            $data = $this->videoAvatarService->getTaskList($user->uid, $page, $pageSize);
            return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }
} 